package com.base.system.managedBean;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.Validate;
import org.operamasks.faces.user.util.Browser;

import com.base.common.config.RepositoryConst;
import com.base.security.SecurityConst;
import com.base.security.entity.Resource;
import com.base.system.managedBean.baseBean.BaseResourceBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "modules.system.webadmin.addResourceBean", scope = ManagedBeanScope.REQUEST)
public class AddResourceBean extends BaseResourceBean {
	Log logger = LogFactory.getLog(AddResourceBean.class);

	@Action(id = "saveResource")
	public void saveResource() {
		// if (resource == null)
		// resource = (Resource) context.getExternalContext().getSessionMap()
		// .get(SecurityConst.RESOURCE_TOCREATE);
		resource = new Resource();
		setResourceProperty();
		securityEntityManager.saveResource(resource);
		// 广播一个新增通知的事件，让noticeListBean中的新增通知Listener在列表中的新增一条通知
		// event.broadcast(this, "addNotice", notice);
		String script = "closeCenterLayoutTab('"
				+ SecurityConst.RESOURCE_ADD_TAB + "')";
		Browser.execClientScript("window.top", script);
	}

	@Validate(id = "code", message = "代码名重复或者输入了非英文字符")
	private boolean validateCode(String code) {
		List list = securityEntityManager.getResourcesByCode(code);
		boolean result = (list == null || list.size() == 0);
		if (result) {
			Pattern p = Pattern
					.compile(RepositoryConst.REPISITORY_REGULAR_UNDERLINE_ENGLISH);
			Matcher m = p.matcher(code);
			return m.matches();
		} else {
			return false;
		}
	}
}
